package com.xusheng.cli.controller.back;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.feedbackModule.Feedback;
import com.xusheng.cli.entity.frontUserModule.FrontUser;
import com.xusheng.cli.service.FeedbackService;
import com.xusheng.cli.service.FrontUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * (Feedback)表控制层
 *
 * @author whr
 * @since 2023-11-16 09:47:00
 */
@Validated
@RestController
@RequestMapping("/api/v1/feedback")
public class FeedbackController {
    /**
     * 服务对象
     */
    @Resource
    private FeedbackService feedbackService;
    @Resource
    private FrontUserService frontUserService;

    /**
     * 查询所有数据
     *
     * @param feedback 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-11-16 09:47:00
     */
    @GetMapping("queryAll")
    public Result<List<Feedback>> queryAll(Feedback feedback) {
        return Result.success(feedbackService.list(new QueryWrapper<>(feedback)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page     分页对象
     * @param feedback 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-11-16 09:47:00
     */
    @GetMapping("queryByPage")
    public Result<MyPage<Feedback>> queryByPage(MyPage<Feedback> page, Feedback feedback) {
        MyPage<Feedback> page1 = feedbackService.page(page, new LambdaQueryWrapper<>(feedback)
                .gt(feedback.getStartTime() != null, Feedback::getCreateTime, feedback.getStartTime())
                .lt(feedback.getEndTime() != null, Feedback::getCreateTime, feedback.getEndTime()));
        List<Feedback> records = page1.getRecords();
        for (Feedback feedback1 : records) {
            FrontUser user = frontUserService.getById(feedback1.getUserId());
            feedback1.setUserName(user.getAccount());
//            feedback1.setUserName(DesensitizedUtil.mobilePhone(user.getAccount()));
        }
        return Result.success(page1);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author whr
     * @since 2023-11-16 09:47:00
     */
    @GetMapping("queryOne/{id}")
    public Result<Feedback> selectOne(@PathVariable Serializable id) {
        return Result.success(feedbackService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param feedback 实体对象
     * @return 新增结果
     * @author whr
     * @since 2023-11-16 09:47:00
     */
    @PostMapping("insert")
    public Result<Object> insert(@Validated(Save.class) @RequestBody Feedback feedback) {
        return Result.isSuccess(feedbackService.save(feedback));
    }

    /**
     * 修改数据
     *
     * @param feedback 实体对象
     * @return 修改结果
     * @author whr
     * @since 2023-11-16 09:47:00
     */
    @PutMapping("update")
    public Result<Object> update(@Validated(Update.class) @RequestBody Feedback feedback) {
        return Result.isSuccess(feedbackService.updateById(feedback));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author whr
     * @since 2023-11-16 09:47:00
     */
    @DeleteMapping("delete/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        Feedback feedback = new Feedback();
        feedback.setId(id);
        return Result.isSuccess(feedbackService.removeById(feedback));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author whr
     * @since 2023-11-16 09:47:00
     */
    @DeleteMapping("batchDelete")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(feedbackService.batchDelete(idList, new Feedback()));
    }
}

